package com.pactera.asmp.server.restfullapi;

import com.pactera.asmp.server.common.annotations.JwtIgnore;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.exception.CustomException;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfulFileUploadApi
 * @Description: RestfulFileUploadApi
 * @Date 2020/07/05 17:46
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class RestfulFileUploadApi {

    @Autowired
    private MinioUtils minioUtils;

    @JwtIgnore
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseResult upload(@RequestParam("file") MultipartFile file) {
        ResponseResult responseResult;
        if(file == null){
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        String fileName;
        try {
            fileName = minioUtils.uploadFile(file);
        } catch (Exception e) {
            log.error("dbc file upload error: ", e);
            throw new CustomException(ResultCode.UPLOAD_ERROR);
        }

        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(fileName);
        return responseResult;
    }

    @RequestMapping(value = "download")
    public void download(@RequestParam("fileName") String filename)  {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        // 设置信息给客户端不解析
        String type = new MimetypesFileTypeMap().getContentType(filename);
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        response.setHeader("Content-type",type);
        // 设置编码
        // 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        try {
            InputStream myStream = minioUtils.getObjectInputStream(filename);
            IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
            myStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @RequestMapping(value = "/downloadUrl", method = RequestMethod.GET)
//    public String downloadUrl(@RequestParam("fileId") String fileId) {
//        String url = "";
//        try {
//            url = client.getFile(fileId);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return url;
//    }
//
//    @RequestMapping(value = "/multi/upload", method = RequestMethod.POST)
//    public List<SeaweedFsClient.SeaweedFsResponse> handleFileUpload(HttpServletRequest request) {
//        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
//        List<SeaweedFsClient.SeaweedFsResponse> responses = new ArrayList<>();
//        if (files.size() != 0) {
//            for (MultipartFile temp : files) {
//                try {
//                    SeaweedFsClient.SeaweedFsResponse response = client.saveFile(temp);
//                    responses.add(response);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return responses;
//    }
}
